home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 January / january_2001.iso / intercd / root / ^4Developers / VB / visbasdb / VBDB / VBDB Code / VB5 Code / Class 6 / Example6-8.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-09-18  |  9.4 KB  |  329 lines

  1. VERSION 5.00
  2. Begin VB.Form frmAuthors 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Authors"
  5.    ClientHeight    =   3060
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4680
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   3060
  13.    ScaleWidth      =   4680
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.CommandButton cmdHelp 
  16.       Caption         =   "&Help"
  17.       Height          =   375
  18.       Left            =   3240
  19.       TabIndex        =   14
  20.       TabStop         =   0   'False
  21.       Top             =   2640
  22.       Width           =   1215
  23.    End
  24.    Begin VB.Data datAuthors 
  25.       Caption         =   "Data1"
  26.       Connect         =   "Access"
  27.       DatabaseName    =   "C:\VBDB\Working\Biblio.mdb"
  28.       DefaultCursorType=   0  'DefaultCursor
  29.       DefaultType     =   2  'UseODBC
  30.       Exclusive       =   0   'False
  31.       Height          =   300
  32.       Left            =   2520
  33.       Options         =   0
  34.       ReadOnly        =   0   'False
  35.       RecordsetType   =   1  'Dynaset
  36.       RecordSource    =   "SELECT * FROM Authors ORDER BY Author"
  37.       Top             =   120
  38.       Visible         =   0   'False
  39.       Width           =   1980
  40.    End
  41.    Begin VB.CommandButton cmdDone 
  42.       Caption         =   "Do&ne"
  43.       Height          =   375
  44.       Left            =   3240
  45.       TabIndex        =   13
  46.       TabStop         =   0   'False
  47.       Top             =   2160
  48.       Width           =   1215
  49.    End
  50.    Begin VB.CommandButton cmdDelete 
  51.       Caption         =   "&Delete"
  52.       Height          =   375
  53.       Left            =   1680
  54.       TabIndex        =   12
  55.       TabStop         =   0   'False
  56.       Top             =   2160
  57.       Width           =   1215
  58.    End
  59.    Begin VB.CommandButton cmdEdit 
  60.       Caption         =   "&Edit"
  61.       Height          =   375
  62.       Left            =   120
  63.       TabIndex        =   11
  64.       TabStop         =   0   'False
  65.       Top             =   1680
  66.       Width           =   1215
  67.    End
  68.    Begin VB.CommandButton cmdCancel 
  69.       Caption         =   "&Cancel"
  70.       Height          =   375
  71.       Left            =   3240
  72.       TabIndex        =   10
  73.       TabStop         =   0   'False
  74.       Top             =   1680
  75.       Width           =   1215
  76.    End
  77.    Begin VB.CommandButton cmdSave 
  78.       Caption         =   "&Save"
  79.       Height          =   375
  80.       Left            =   1680
  81.       TabIndex        =   9
  82.       TabStop         =   0   'False
  83.       Top             =   1680
  84.       Width           =   1215
  85.    End
  86.    Begin VB.CommandButton cmdAddNew 
  87.       Caption         =   "&Add New"
  88.       Height          =   375
  89.       Left            =   120
  90.       TabIndex        =   8
  91.       TabStop         =   0   'False
  92.       Top             =   2160
  93.       Width           =   1215
  94.    End
  95.    Begin VB.CommandButton cmdNext 
  96.       Caption         =   "Next =>"
  97.       Height          =   255
  98.       Left            =   2520
  99.       TabIndex        =   7
  100.       TabStop         =   0   'False
  101.       Top             =   1320
  102.       Width           =   1215
  103.    End
  104.    Begin VB.CommandButton cmdPrevious 
  105.       Caption         =   "<= Previous"
  106.       Height          =   255
  107.       Left            =   1080
  108.       TabIndex        =   6
  109.       TabStop         =   0   'False
  110.       Top             =   1320
  111.       Width           =   1215
  112.    End
  113.    Begin VB.TextBox txtYearBorn 
  114.       DataField       =   "Year Born"
  115.       DataSource      =   "datAuthors"
  116.       Height          =   285
  117.       Left            =   1200
  118.       Locked          =   -1  'True
  119.       MaxLength       =   4
  120.       TabIndex        =   2
  121.       Text            =   "Text"
  122.       Top             =   840
  123.       Width           =   1215
  124.    End
  125.    Begin VB.TextBox txtAuthor 
  126.       DataField       =   "Author"
  127.       DataSource      =   "datAuthors"
  128.       Height          =   285
  129.       Left            =   1200
  130.       Locked          =   -1  'True
  131.       TabIndex        =   1
  132.       Text            =   "Text2"
  133.       Top             =   480
  134.       Width           =   3375
  135.    End
  136.    Begin VB.TextBox txtAuthorID 
  137.       DataField       =   "Au_ID"
  138.       DataSource      =   "datAuthors"
  139.       Height          =   285
  140.       Left            =   1200
  141.       Locked          =   -1  'True
  142.       TabIndex        =   3
  143.       TabStop         =   0   'False
  144.       Text            =   "Text1"
  145.       Top             =   120
  146.       Width           =   1215
  147.    End
  148.    Begin VB.Label Label3 
  149.       Caption         =   "Year Born"
  150.       Height          =   255
  151.       Left            =   120
  152.       TabIndex        =   5
  153.       Top             =   840
  154.       Width           =   1215
  155.    End
  156.    Begin VB.Label Label2 
  157.       Caption         =   "Author Name"
  158.       Height          =   255
  159.       Left            =   120
  160.       TabIndex        =   4
  161.       Top             =   480
  162.       Width           =   1215
  163.    End
  164.    Begin VB.Label Label1 
  165.       Caption         =   "Author ID"
  166.       Height          =   255
  167.       Left            =   120
  168.       TabIndex        =   0
  169.       Top             =   120
  170.       Width           =   1215
  171.    End
  172. Attribute VB_Name = "frmAuthors"
  173. Attribute VB_GlobalNameSpace = False
  174. Attribute VB_Creatable = False
  175. Attribute VB_PredeclaredId = True
  176. Attribute VB_Exposed = False
  177. Option Explicit
  178. Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" (ByVal hwnd As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal dwData As Long) As Long
  179. Private Const HELP_FINDER = &HB&
  180. Private Sub ValidateData(AllOK As Boolean)
  181. Dim Message As String
  182. Dim InputYear As Integer, CurrentYear As Integer
  183. AllOK = True
  184. Message = ""
  185. 'Check for name
  186. If Len(txtAuthor.Text) = 0 Then
  187.   Message = "You must enter an Author Name." + vbCrLf
  188.   txtAuthor.SetFocus
  189.   AllOK = False
  190. End If
  191. 'Check length and range on Year Born
  192. InputYear = Val(txtYearBorn.Text)
  193. CurrentYear = Val(Format(Now, "yyyy"))
  194. If Len(txtYearBorn.Text) <> 0 Then
  195.   If InputYear > CurrentYear Or InputYear < CurrentYear - 150 Then
  196.     Message = Message + "Year Born must be between" & Str(CurrentYear - 150) & " and" & Str(CurrentYear) & "."
  197.     txtYearBorn.SetFocus
  198.     AllOK = False
  199.   End If
  200. End If
  201. If Not (AllOK) Then
  202.   MsgBox Message, vbOKOnly + vbInformation, "Validation Error"
  203. End If
  204. End Sub
  205. Private Sub cmdAddNew_Click()
  206. On Error GoTo HandleErrors
  207. Call SetState("Add")
  208. Exit Sub
  209. HandleErrors:
  210. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  211. Case vbAbort
  212.   Exit Sub
  213. Case vbRetry
  214.   Resume
  215. Case vbIgnore
  216.   Resume Next
  217. End Select
  218. End Sub
  219. Private Sub cmdCancel_Click()
  220. Call SetState("View")
  221. End Sub
  222. Private Sub cmdDelete_Click()
  223. Dim Response As Integer
  224. On Error GoTo HandleErrors
  225. Response = MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete")
  226. If Response = vbNo Then
  227.   Exit Sub
  228. End If
  229. Exit Sub
  230. HandleErrors:
  231. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  232. Case vbAbort
  233.   Exit Sub
  234. Case vbRetry
  235.   Resume
  236. Case vbIgnore
  237.   Resume Next
  238. End Select
  239. End Sub
  240. Private Sub cmdEdit_Click()
  241. Call SetState("Edit")
  242. End Sub
  243. Private Sub cmdHelp_Click()
  244. Dim Rtn As Long
  245. Rtn = WinHelp(frmAuthors.hwnd, App.HelpFile, HELP_FINDER, CLng(0))
  246. End Sub
  247. Private Sub cmdNext_Click()
  248. datAuthors.Recordset.MoveNext
  249. If datAuthors.Recordset.EOF Then
  250.   Beep
  251.   datAuthors.Recordset.MoveLast
  252. End If
  253. End Sub
  254. Private Sub cmdPrevious_Click()
  255. datAuthors.Recordset.MovePrevious
  256. If datAuthors.Recordset.BOF Then
  257.   Beep
  258.   datAuthors.Recordset.MoveFirst
  259. End If
  260. End Sub
  261. Private Sub cmdSave_Click()
  262. Dim Valid As Boolean
  263. Call ValidateData(Valid)
  264. If Not (Valid) Then Exit Sub
  265. On Error GoTo HandleErrors
  266. MsgBox "Record saved.", vbOKOnly + vbInformation, "Save"
  267. Call SetState("View")
  268. Exit Sub
  269. HandleErrors:
  270. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  271. Case vbAbort
  272.   Exit Sub
  273. Case vbRetry
  274.   Resume
  275. Case vbIgnore
  276.   Resume Next
  277. End Select
  278. End Sub
  279. Private Sub SetState(AppState As String)
  280. Select Case AppState
  281. Case "View"
  282.   txtAuthorID.BackColor = vbWhite
  283.   txtAuthor.Locked = True
  284.   txtYearBorn.Locked = True
  285.   cmdPrevious.Enabled = True
  286.   cmdNext.Enabled = True
  287.   cmdAddNew.Enabled = True
  288.   cmdSave.Enabled = False
  289.   cmdCancel.Enabled = False
  290.   cmdEdit.Enabled = True
  291.   cmdDelete.Enabled = True
  292.   cmdDone.Enabled = True
  293.   txtAuthor.SetFocus
  294. Case "Add", "Edit"
  295.   txtAuthorID.BackColor = vbRed
  296.   txtAuthor.Locked = False
  297.   txtYearBorn.Locked = False
  298.   cmdPrevious.Enabled = False
  299.   cmdNext.Enabled = False
  300.   cmdAddNew.Enabled = False
  301.   cmdSave.Enabled = True
  302.   cmdCancel.Enabled = True
  303.   cmdEdit.Enabled = False
  304.   cmdDelete.Enabled = False
  305.   cmdDone.Enabled = False
  306.   txtAuthor.SetFocus
  307. End Select
  308. End Sub
  309. Private Sub Form_Activate()
  310. Call SetState("View")
  311. End Sub
  312. Private Sub txtAuthor_KeyPress(KeyAscii As Integer)
  313. If KeyAscii = vbKeyReturn Then
  314.   txtYearBorn.SetFocus
  315.   Exit Sub
  316. End If
  317. End Sub
  318. Private Sub txtYearBorn_KeyPress(KeyAscii As Integer)
  319. If KeyAscii = vbKeyReturn Then
  320.   txtAuthor.SetFocus
  321.   Exit Sub
  322. End If
  323. If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or KeyAscii = vbKeyBack Then
  324.   Exit Sub
  325.   Beep
  326.   KeyAscii = 0
  327. End If
  328. End Sub
  329.